﻿/* File Created: 十一月 11, 2013 */
var jsonObj = null;
var drawingManager = null;
var overlays = [];

function InitFrame() {
    var height = GetWindowHeight();
    var bannerHeight = document.getElementById("header").offsetHeight;
    document.getElementById("content").style.height = (height - bannerHeight) + "px";
    document.getElementById("datagrid").style.height = document.getElementById("content").style.height;
    document.getElementById("map").style.height = document.getElementById("content").style.height;
    document.getElementById("data").style.height = document.getElementById("content").style.height;
}

function initMap() {
    //初始化地图对象
    map = new BMap.Map("map");
    var point = new BMap.Point(108, 35);  // 创建点坐标  
    map.centerAndZoom(point, 5);                 // 初始化地图，设置中心点坐标和地图级别 
    map.addControl(new BMap.NavigationControl());    
    map.addControl(new BMap.ScaleControl());    
    map.addControl(new BMap.OverviewMapControl());    
    map.addControl(new BMap.MapTypeControl()); 
    map.enableScrollWheelZoom();                            //启用滚轮放大缩小

    // 初始化绘制
    var styleOptions = {
        strokeColor:"red",    //边线颜色。
        fillColor:"red",      //填充颜色。当参数为空时，圆形将没有填充效果。
        strokeWeight: 3,       //边线的宽度，以像素为单位。
        strokeOpacity: 0.8,	   //边线透明度，取值范围0 - 1。
        fillOpacity: 0.6,      //填充的透明度，取值范围0 - 1。
        strokeStyle: 'solid' //边线的样式，solid或dashed。
    }

    //实例化鼠标绘制工具
    drawingManager = new BMapLib.DrawingManager(map, {
        isOpen: false, //是否开启绘制模式
        enableDrawingTool: true, //是否显示工具栏
        drawingToolOptions: {
            anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
            offset: new BMap.Size(200, 5), //偏离值
            scale: 0.8,                    //工具栏缩放比例
            drawingModes: [BMAP_DRAWING_RECTANGLE]
        },
        rectangleOptions: styleOptions //矩形的样式
    });

    drawingManager.addEventListener('rectanglecomplete', onDrawRect);
}

function InitDataGrid() {
    $('#data').datagrid({
        url: null,
        pagination: true,
        loadMsg: '数据加载中请稍后……',
        singleSelect:true,
        columns: [[
                { field: '档号', title: '档号', width: 80 },
                { field: '题名', title: '题名', width: 200 },
                { field: '详情', title: '详情', align: 'right',formatter:function(value,row,index)
                {  
                    var html = '<a href="GeoDataDetails.aspx?fid=' + row["档号"] + '" target="_blank" style="height:20px;width:60px;">详情</a>';
                    return html;
                } }]],
        onDblClickRow: function (index, rowData) {
            _selectRow(index, rowData);
        }
    });

    $.get("service/SpatialQuerySvc.aspx", { Method: "GetAllInfo" }, function (data) {
        FillData(data);
    });
}

function GetWindowHeight() {
    var winHeight = 0;

    //获取窗口高度
    if (window.innerHeight)
        winHeight = window.innerHeight;
    else if ((document.body) && (document.body.clientHeight))
        winHeight = document.body.clientHeight;

    //通过深入Document内部对body进行检测，获取窗口大小
    if (document.documentElement
               && document.documentElement.clientHeight) {
        winHeight = document.documentElement.clientHeight;
    }
    //结果输出
    return winHeight;
}

function FillData(data) {
    var jsonStr = data.replace(/\r\n/ig, "<br \/>");
    var dataList = [];

    jsonObj = eval(jsonStr);
    map.clearOverlays();
    
    for (var i = 0; i < jsonObj.length; i++) {
        var item = {
            "档号": jsonObj[i].FILE_ID,
            "题名": jsonObj[i].TITLE
        };

        dataList.push(item);

        //添加
        overlays.push(CreateInfoWin(jsonObj[i].START_LATITUDE,jsonObj[i].START_LONGITUDE,jsonObj[i].FILE_ID,jsonObj[i].TITLE));
    }

    $('#data').datagrid("loadData", dataList);
}

function _selectRow(index, data) {
    var xy = GetXY(data.档号);
    var point = new BMap.Point(Trans(xy.y), Trans(xy.x));
    //map.centerAndZoom(point, 15);
    map.panTo(point);
    overlays[index].infoWindow.open(overlays[index]);
}

// 创建InfoWindow
function CreateInfoWin(x, y, fileID, title) {
    var sContent = '<div style="font-size:12px;"><br/>' + "<strong>题名: </strong>" + title + '<br/>' + "<strong>档号: </strong>" + fileID +
    '<p style="text-align:center;margin-top:20px;"><a  href="GeoDataDetails.aspx?FID=' + fileID + 
    '"target="_blank" style="color:red;">详情</a></p></div>';
    var point = new BMap.Point(Trans(y), Trans(x));
    var marker = new BMap.Marker(point);
    var searchInfoWindow = null;

	searchInfoWindow = new BMapLib.SearchInfoWindow(map, sContent, {
	        title: "题名：" + title,          //标题
			width  : 150,             //宽度
			height : 100,             //高度
			panel  : "panel",         //检索结果面板
			enableAutoPan : true,     //自动平移
			enableSendToPhone:false,
			searchTypes   :[]
	});

    marker.infoWindow = searchInfoWindow;
    //map.centerAndZoom(point, 15);
    map.addOverlay(marker);
    marker.addEventListener("click", function(e){  
        e.target.infoWindow.open(this);     
        //this.openInfoWindow(e.target.infoWindow);
    });
   
    return marker;
}

function GetXY(file_id) {
    for(var i=0; i<jsonObj.length; i++) {
        if (file_id == jsonObj[i].FILE_ID)
            return { x: jsonObj[i].START_LATITUDE, y: jsonObj[i].START_LONGITUDE }; 
    }
}

// 将数据库中存储的经纬度转为真实的经纬度
function Trans(value) {
    var a = Math.floor(value / 10000);
    var b = Math.floor(value % 10000 / 100);
    var c = Math.floor(value % 100);
    return a + (b + (c / 60)) / 60;
}

// 范围查询
function onDrawRect(overlay) {
    var rect = overlay.getBounds();

    map.removeOverlay(overlay);
    drawingManager.close();
    var xmin = rect.Tf.lng;
    var ymin = rect.Tf.lat;
    var xmax = rect.Lf.lng;
    var ymax = rect.Lf.lat;

    $.get("service/SpatialQuerySvc.aspx", { Method: "GetInfoByRect",XMIN:xmin,YMIN:ymin,XMAX:xmax,YMAX:ymax}, function (data) {
        FillData(data);
    });
}